package com.project.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.project.config.LoginThreadLocal;
import com.project.dto.UserDto;
import com.project.dto.UserResultDto;
import com.project.entity.UserEntity;
import com.project.entity.UserRecordEntity;
import com.project.mapper.IUserMapper;
import com.project.mapper.IUserRecordMapper;
import com.project.service.IUserService;
import com.project.util.ConvertBeanUtil;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.util.List;

@Service
@Transactional
public class UserServiceImpl implements IUserService {

    @Autowired
    RabbitTemplate rabbitTemplate;
    @Autowired
    private IUserMapper mapper;
    @Autowired
    private IUserRecordMapper recordMapper;

//    @RabbitListener(queues = "AU")
//    public void orderInfo(Channel channel, @Header(AmqpHeaders.DELIVERY_TAG) long tag, String json) throws IOException {
////        把JSon转换为对象
//        System.err.println("json = " + json);
//        if(StringUtils.hasLength(json)) {
//          UserEntity user=
//                    JSONObject.parseObject(json, UserEntity.class);
//            mapper.insert(user);
//            UserRecordEntity recordEntity =
//                    JSONObject.parseObject(json, UserRecordEntity.class);
//            recordMapper.insert(recordEntity);
//        }
//        //通知业务处理完成
//        channel.basicAck(tag, false); //tag标志，false-不批量确认
//    }

    @Override
    public IPage<UserEntity> findByItem(int pageNO, int verifyStatus, int pageSize) {
        QueryWrapper<UserEntity> qw = new QueryWrapper<>();
        qw.eq("u_verifyStatus", verifyStatus);
        return mapper.selectPage(new Page<>(pageNO, pageSize), qw);
    }

    @Override
    public UserRecordEntity findById(long id) {
        UserEntity userEntity = mapper.selectById(id);
        QueryWrapper<UserRecordEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("u_num", userEntity.getUserRef());
        queryWrapper.eq("u_status", 0);
        return recordMapper.selectOne(queryWrapper);
    }

    @Override
    public List<UserRecordEntity> findByHistory(long id) {
        UserEntity userEntity = mapper.selectById(id);

        QueryWrapper<UserRecordEntity> qw = new QueryWrapper<>();
        qw.eq("u_num", userEntity.getUserRef());
        qw.ne("u_status", 0);
        return recordMapper.selectList(qw);
    }

    @Override
    public UserEntity findByUserRef(Long userRef, Double addMoney) {
        QueryWrapper<UserEntity> qw = new QueryWrapper<>();
        qw.eq("u_num", userRef);
        UserEntity user = mapper.selectOne(qw);
        user.setBalance(user.getBalance() + addMoney);
        mapper.updateById(user);
        return user;
    }

    @Override
    public void updateStatus(long customerId, int u_verifyStatus, String cause) {

        UserRecordEntity userRecordEntity = recordMapper.selectById(customerId);
        userRecordEntity.setVerifyName(LoginThreadLocal.getLoginVerify().getName());
        userRecordEntity.setCause(cause);
        userRecordEntity.setStatus(u_verifyStatus);
        recordMapper.updateById(userRecordEntity);

        QueryWrapper<UserEntity> qw = new QueryWrapper<>();
        qw.eq("u_num", userRecordEntity.getUserRef());
        UserEntity user = mapper.selectOne(qw);
        user.setVerifyStatus(u_verifyStatus);
        mapper.updateById(user);

        //封装审核结果L
        UserResultDto userResultDto = new UserResultDto();
        userResultDto.setUserNum(userRecordEntity.getUserRef());
        userResultDto.setResult(u_verifyStatus);
        userResultDto.setDate(LocalDate.now());
        userResultDto.setReason(cause);
        String jsonObject = JSONObject.toJSONString(userResultDto);
        rabbitTemplate.convertAndSend("topicExchangeTwo", "rabbit.UserVerifyResult", jsonObject);




        if (u_verifyStatus == 1) {
            UserDto userDto = ConvertBeanUtil.copy(userRecordEntity, UserDto.class);
            userDto.setUserNum(userRecordEntity.getUserRef());
            userDto.setUserName(user.getName());
            userDto.setPassword(user.getPwd());
            userDto.setIDImg(userRecordEntity.getIdentity());
            userDto.setQuaImg(userRecordEntity.getImage());
            String userVerifySuccess = JSONObject.toJSONString(userDto);
            rabbitTemplate.convertAndSend("topicExchangeTwo", "rabbit.UserVerifySuccess", userVerifySuccess);
        }
    }
}
